﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebPad
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class PadRequest : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string strid = context.Request["id"];
            string strname = context.Request["name"];

            string str = "{\"Error\":\"1\", ";
            switch( strid )
            {
                case "1":   // 遥控器页面，发送单个AGV信息，遥控器用
                {
                    try
                    {
                        int num = int.Parse(strname);
                        str = getAGV(num);
                    }
                    catch(Exception)
                    {
                        str += "\"str\":\"上传AGV编号格式错误\"}";
                    }
                    break;
                }
                case "2":   // AGV状态页面，发送全部AGV信息
                {
                    str = getStatus();
                    break;
                }
                case "3":   // 日志页面，发送日志
                {
                    str = getLog();
                    break;
                }
                case "4":   // 实时页面，发送全部AGV信息
                {
                    str = getStatus();
                    break;
                }
                default:
                {
                    str += "\"str\":\"请求ID无效，" + strid + "\"}";
                    break;
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(str);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 获取指定的AGV数据
        /// </summary>
        /// <param name="Num">AGV编号</param>
        private string getAGV(int Num)
        {
            string str = "";
            DateTime dt = DateTime.Now;
            if (Num == 18)
            {
                str = "{ " +
                    "\"id\":\"" + 1 + "\", " +
                    "\"name\":\"" + Num + "\", " +
                    "\"speed\":\"" + dt.Year + "\", " +
                    "\"soc\":\"" + dt.Month + "\", " +
                    "\"state\":\"" + dt.Day + "\", " +
                    "\"x\":\"" + dt.Hour + "\", " +
                    "\"y\":\"" + dt.Minute + "\", " +
                    "\"angle\":\"" + dt.Second + "\", " +
                    "\"load\":\"" + (1500 + Num) + "\" " +  // 最后一项不能用逗号
                    "}";
            }
            else
                str += "\"str\":\"上传AGV编号无效，" + Num + "\"}";

            return str;
        }

        /// <summary>
        /// 获取全部AGV信息
        /// </summary>
        private string getStatus()
        {
            DateTime dt = DateTime.Now;
            string str = "{";
            for (int i = 0; i < 10; i++)
            {
                str += "\"" + i + "\":{ " +
                    "\"number\":\"" + i + "\", " +
                    "\"type\":\"" + "QF2T" + "\", " +
                    "\"speed\":\"" + dt.Year + "\", " +
                    "\"soc\":\"" + dt.Month + "\", " +
                    "\"state\":\"" + dt.Day + "\", " +
                    "\"x\":\"" + dt.Hour + "\", " +
                    "\"y\":\"" + dt.Minute + "\", " +
                    "\"angle\":\"" + dt.Second + "\", " +
                    "\"data\":\"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\", " +
                    "\"load\":\"" + (1500 + i) + "\" " +  // 最后一项不能用逗号
                    "}";
                if (i != 9)
                    str += ", ";
                else
                    str += "}";
            }

            return str;
        }

        /// <summary>
        /// 获取运行日志
        /// </summary>
        private string getLog()
        {
            DateTime dt = DateTime.Now;
            string str = "{";
            for (int i = 0; i < 10; i++)
            {
                str += "\"" + i + "\":{ " +
                    "\"datetime\":\"" + dt.ToLongTimeString() + "\", " +
                    "\"code\":\"" + i + "01010\", " +
                    "\"note\":\"AGV通讯中断\"" +
                    "}";
                if (i != 9)
                    str += ", ";
                else
                    str += "}";
            }
            return str;
        }
    }
}